<?php

class ManageTicketTemplateController extends PsController {

	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'users' => array('@'),
						//'roles' => array('Transactions.ViewOwn'),
					)), parent::accessRules());
	}

	public function moduleAccessRules() {
		return array();
	}
	
	public function actionView($id) {
		$this->render('view', array(
			'ticketTemplate' => $this->loadTicketTemplate($id),
		));
	}

	public function actionCreate() {
		$ticketTemplate = new TicketTemplate;

		if (isset($_POST['TicketTemplate'])) {
			$ticketTemplate->setAttributes($_POST['TicketTemplate']);
			$ticketTemplate->agency_id = Yii::app()->user->model->agency_id;
			if ($ticketTemplate->save()) {
				if (Yii::app()->getRequest()->getIsAjaxRequest()) {
					Yii::app()->end();
				}
				$this->redirect(array('view', 'id' => $ticketTemplate->id));
			} else {	
				//dump ($ticketTemplate->)
				//dump ($ticketTemplate->getErrors()); exit; 
			}
		}

		$this->render('create', array( 'ticketTemplate' => $ticketTemplate));
	}

	public function actionUpdate($id) {
		$ticketTemplate = $this->loadTicketTemplate($id);


		if (isset($_POST['TicketTemplate'])) {
			$ticketTemplate->setAttributes($_POST['TicketTemplate']);

			if ($ticketTemplate->save()) {
				$this->redirect(array('view', 'id' => $ticketTemplate->id));
			}
		}

		$this->render('update', array(
				'ticketTemplate' => $ticketTemplate,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadTicketTemplate($id)->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$ticketTemplate = new TicketTemplate('search');
		$ticketTemplate->unsetAttributes();

		if (isset($_GET['TicketTemplate']))
			$ticketTemplate->setAttributes($_GET['TicketTemplate']);

		$this->render('index', array(
			'ticketTemplate' => $ticketTemplate,
		));
	}
	
	/**
	 * Loads the TicketTemplate AR model
	 * @param integer $id the primary key
	 * @return TicketTemplate	 */
	public function loadTicketTemplate($id) {
		//$ticketTemplate = TicketTemplate::model()->ofCurrentUser()->findByPk($id);
		$ticketTemplate = TicketTemplate::model()->findByPk($id);

		if ($ticketTemplate == null) {
			throw new PsPageNotFoundException();
		}

		return $ticketTemplate;
	}
}